<div class="layui-card">
    <div class="layui-card-body" style="border-bottom: 1px solid #f6f6f6;">
        <div class="layui-row">
            <div class="layui-btn-group larry-group">
                <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="javascript:layer.open({title:'增加',type:2,area: ['100%', '100%'],content:'<?=url('patchcat/add')?>'});">
                    <i class="layui-icon">&#xe654;</i>
                    <cite>增加</cite>
                </button>
            </div>
        </div>
    </div>
    <div class="layui-card-body">
        <div class="layui-row layui-col-space15">
                <!--列表样式-->
                <?php foreach($list as $v):?>
                  <div class="layui-col-md4">
                      <fieldset class="layui-elem-field">
                          <legend><?=$v['cat_title']?>-<?=$v['uuid']?></legend>
                          <form class="layui-field-box layui-form layui-form-pane" action="">
                            <div class="layui-form-item">
                              <button class="layui-btn layui-btn-primary layui-btn-sm" onclick="javascript:layer.open({title:'增加',type:2,area: ['50%', '50%'],content:'<?=url('patch/add',['cat_id'=>$v['id']])?>'});"><i class="layui-icon"></i></button>
                            </div>
                            <?php foreach($v['childlist'] as $vv):?>
                                    <div class="layui-form-item">
                                    <label class="layui-form-label"><?=$vv["title"]?></label>
                                    <div class="layui-input-block">
                                        <div class="layui-col-xs9">
                                            <?php if($vv['type'] == 0):?>
                                                <input type="text" value="<?=$vv['content']?>" data-id="<?=$vv['id']?>" data-field="content"  onchange="upData(this)" lay-verify="required" placeholder="请输入<?=$vv["title"]?>" autocomplete="off" class="layui-input">
                                            <?php else: ?>
                                                <div class="layui-col-xs6">
                                                <button type="button" class="layui-btn my_pic" data-id="<?=$vv['id']?>" id="pic<?=$vv['id']?>">
                                                  <i class="layui-icon">&#xe67c;</i>上传图片
                                                </button>
                                                </div>
                                                <div class="layui-col-xs6">
                                                    <a target="_blank" href="<?=$vv['content']?>"><img src="<?=$vv['content']?>" height="35" /></a>
                                                </div>
                                            <?php endif;?>
                                        </div>
                                            <div class="layui-col-xs1">
                                                <input type="text" value="<?=$vv['sort']?>"  data-id="<?=$vv['id']?>" data-field="sort"  onchange="upData(this)" class="layui-input">
                                            </div>
                                            <div class="layui-col-xs1" onclick="delData(<?=$vv['id']?>)">
                                                <button class="layui-btn layui-btn-primary"><i class="layui-icon"></i></button>
                                            </div>
                                    </div>
                                  </div>
                            <?php endforeach;?>
                          </form>
                        </fieldset>

                  </div>
                <?php endforeach;?>

        </div>
    </div>
</div>

<script>
//更新字段值
function upData(e){
    var data = {
        id:$(e).attr('data-id'),
        field:$(e).attr('data-field'),
        value:$(e).val()
    };
    var index = layer.load(2, {time: 10*1000});
    $.ajax({
        url:"<?=url('patch/updata')?>",
        method:"post",
        dataType:"json",
        data:data,
        success:function(res){
            location.reload();
        },
        complete: function(){
            layer.close(index);
        }
    });
}
//删除
function delData(id){
    layer.confirm('确认要删除吗?', function(index){
    var index = layer.load(2, {time: 10*1000});
      $.ajax({
          url:"/patch/del",
          method:"post",
          dataType:"json",
          data:{id:id},
          success:function(res){
              location.reload();
          },
          complete: function(){
              layer.close(index);
          }
      });
    });
}
layui.use(['form','upload'], function(){
  var $ = layui.$
  ,upload = layui.upload
  ,form = layui.form;
  form.render();

  //上传图片
  $(".my_pic").each(function(){
      var id =  $(this).attr('data-id');
      upload.render({
          elem: $(this) //绑定元素
          ,url: '/api/upimg/' //上传接口
          ,done:function(res){
              var pic = res.data.url;
              var index = layer.load(2, {time: 10*1000});
              var data = {
                  id:id,
                  field:'content',
                  value:pic
              };
              $.ajax({
                  url:"<?=url('patch/updata')?>",
                  method:"post",
                  dataType:"json",
                  data:data,
                  success:function(res){
                      location.reload();
                  },
                  complete: function(){
                      layer.close(index);
                  }
              });
          }
          ,error: function(){
             layer.msg('请求异常');
          }
        });
  });
});
</script>
